﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using DingTalkAgent.DBUtility;

namespace DingTalkAgent.SqlData
{
    public partial class Admin_Info
    {
        #region	 T_Admin_Info
        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <param name="MemberAdmin_Info"></param>
        /// <param name="PassWord"></param>
        /// <returns></returns>
        public DingTalkAgent.Model._view_Admin_Info Logon(string MemberAdmin_Info, string PassWord)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID, account, password, Name, status,RoleID ");
            strSql.Append("  from T_Admin_Info ");
            strSql.Append(" where account=@account and password=@password");
            SqlParameter[] parameters = {
                    new SqlParameter("@account", SqlDbType.VarChar,40),
                    new SqlParameter("@password", SqlDbType.VarChar,40)
            };
            parameters[0].Value = MemberAdmin_Info;
            parameters[1].Value = PassWord;
            DingTalkAgent.Model._view_Admin_Info model = new DingTalkAgent.Model._view_Admin_Info();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                    model.UserID = model.ID;
                    model.RoleID = int.Parse(ds.Tables[0].Rows[0]["RoleID"].ToString());
                }
                model.account = ds.Tables[0].Rows[0]["account"].ToString();
                //model.password = ds.Tables[0].Rows[0]["password"].ToString();
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                if (ds.Tables[0].Rows[0]["status"].ToString() != "")
                {
                    model.status = int.Parse(ds.Tables[0].Rows[0]["status"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Password"></param>
        /// <param name="oldPassword"></param>
        /// <returns></returns>
        public bool UpdatePassWord(int ID, string Password, string oldPassword)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Admin_Info set ");
            strSql.Append(" password = @Password ");

            strSql.Append(" where ID=@ID and password=@oldPassword");

            SqlParameter[] parameters = {
                        new SqlParameter("@ID", SqlDbType.Int,4) ,
                        new SqlParameter("@Password", SqlDbType.VarChar,40) ,
                        new SqlParameter("@oldPassword", SqlDbType.VarChar,40)

            };

            parameters[0].Value = ID;
            parameters[1].Value = Password;
            parameters[2].Value = oldPassword;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        ///<summary>
        /// Exist
        ///</summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Admin_Info");
            strSql.Append(" where ");
            strSql.Append(" ID = @ID  ");
            SqlParameter[] parameters = {
                    new SqlParameter("@ID", SqlDbType.Int,4)
            };
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public bool ExistsAdmin(string account)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Admin_Info");
            strSql.Append(" where ");
            strSql.Append(" account = @account  ");
            SqlParameter[] parameters = {
                    new SqlParameter("@account", SqlDbType.VarChar,40)
            };
            parameters[0].Value = account;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条T_Admin_Info数据
        /// </summary>
        public int Add(DingTalkAgent.Model.Admin_Info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Admin_Info(");
            strSql.Append("AreaCode,Remark,account,password,Phone,Name,RoleID,LoginIP,LoginTime,status");
            strSql.Append(") values (");
            strSql.Append("@AreaCode,@Remark,@account,@password,@Phone,@Name,@RoleID,@LoginIP,@LoginTime,@status");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                        new SqlParameter("@AreaCode", SqlDbType.VarChar,256) ,
                        new SqlParameter("@Remark", SqlDbType.VarChar,512) ,
                        new SqlParameter("@account", SqlDbType.VarChar,256) ,
                        new SqlParameter("@password", SqlDbType.VarChar,256) ,
                        new SqlParameter("@Phone", SqlDbType.VarChar,256) ,
                        new SqlParameter("@Name", SqlDbType.VarChar,256) ,
                        new SqlParameter("@RoleID", SqlDbType.Int,4) ,
                        new SqlParameter("@LoginIP", SqlDbType.VarChar,256) ,
                        new SqlParameter("@LoginTime", SqlDbType.DateTime) ,
                        new SqlParameter("@status", SqlDbType.Int,4)

            };

            parameters[0].Value = model.AreaCode;
            parameters[1].Value = model.Remark;
            parameters[2].Value = model.account;
            parameters[3].Value = model.password;
            parameters[4].Value = model.Phone;
            parameters[5].Value = model.Name;
            parameters[6].Value = model.RoleID;
            parameters[7].Value = model.LoginIP;
            parameters[8].Value = model.LoginTime;
            parameters[9].Value = model.status;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(obj);

            }

        }


        /// <summary>
        /// 更新一条T_Admin_Info数据
        /// </summary>
        public bool Update(DingTalkAgent.Model.Admin_Info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Admin_Info set ");

            strSql.Append(" AreaCode = @AreaCode , ");
            strSql.Append(" Remark = @Remark , ");
            strSql.Append(" account = @account , ");
            strSql.Append(" password = @password , ");
            strSql.Append(" Phone = @Phone , ");
            strSql.Append(" Name = @Name , ");
            strSql.Append(" RoleID = @RoleID , ");
            strSql.Append(" LoginIP = @LoginIP , ");
            strSql.Append(" LoginTime = @LoginTime , ");
            strSql.Append(" status = @status  ");
            strSql.Append(" where ID=@ID ");

            SqlParameter[] parameters = {
                        new SqlParameter("@ID", SqlDbType.Int,4) ,
                        new SqlParameter("@AreaCode", SqlDbType.VarChar,256) ,
                        new SqlParameter("@Remark", SqlDbType.VarChar,512) ,
                        new SqlParameter("@account", SqlDbType.VarChar,256) ,
                        new SqlParameter("@password", SqlDbType.VarChar,256) ,
                        new SqlParameter("@Phone", SqlDbType.VarChar,256) ,
                        new SqlParameter("@Name", SqlDbType.VarChar,256) ,
                        new SqlParameter("@RoleID", SqlDbType.Int,4) ,
                        new SqlParameter("@LoginIP", SqlDbType.VarChar,256) ,
                        new SqlParameter("@LoginTime", SqlDbType.DateTime) ,
                        new SqlParameter("@status", SqlDbType.Int,4)

            };

            parameters[0].Value = model.ID;
            parameters[1].Value = model.AreaCode;
            parameters[2].Value = model.Remark;
            parameters[3].Value = model.account;
            parameters[4].Value = model.password;
            parameters[5].Value = model.Phone;
            parameters[6].Value = model.Name;
            parameters[7].Value = model.RoleID;
            parameters[8].Value = model.LoginIP;
            parameters[9].Value = model.LoginTime;
            parameters[10].Value = model.status;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条T_Msg_Info数据
        /// </summary>
        public bool UpdateStatus(int ID, int status)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Admin_Info set ");
            strSql.Append(" status = @status  ");
            strSql.Append(" where ID=@ID ");

            SqlParameter[] parameters = {
                        new SqlParameter("@ID", SqlDbType.Int,4) ,
                        new SqlParameter("@status", SqlDbType.Int,4)

            };

            parameters[0].Value = ID;
            parameters[1].Value = status;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条T_Admin_Info数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Admin_Info ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@ID", SqlDbType.Int,4)
            };
            parameters[0].Value = ID;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条T_Admin_Info数据
        /// </summary>
        public bool Deletes(string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Admin_Info ");
            strSql.Append(" where ID in (" + ids + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DingTalkAgent.Model.Admin_Info GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID, AreaCode, Remark, account, password, Phone, Name, RoleID, LoginIP, LoginTime, status  ");
            strSql.Append("  from T_Admin_Info ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@ID", SqlDbType.Int,4)
            };
            parameters[0].Value = ID;


            DingTalkAgent.Model.Admin_Info model = new DingTalkAgent.Model.Admin_Info();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.AreaCode = ds.Tables[0].Rows[0]["AreaCode"].ToString();
                model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                model.account = ds.Tables[0].Rows[0]["account"].ToString();
                model.password = ds.Tables[0].Rows[0]["password"].ToString();
                model.Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                if (ds.Tables[0].Rows[0]["RoleID"].ToString() != "")
                {
                    model.RoleID = int.Parse(ds.Tables[0].Rows[0]["RoleID"].ToString());
                }
                model.LoginIP = ds.Tables[0].Rows[0]["LoginIP"].ToString();
                if (ds.Tables[0].Rows[0]["LoginTime"].ToString() != "")
                {
                    model.LoginTime = DateTime.Parse(ds.Tables[0].Rows[0]["LoginTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["status"].ToString() != "")
                {
                    model.status = int.Parse(ds.Tables[0].Rows[0]["status"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据T_Admin_Info列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_Admin_Info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据T_Admin_Info列表
        /// </summary>
        public IEnumerable<DingTalkAgent.Model.Admin_Info> IEnumGetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID, AreaCode, Remark, account, password, Phone, Name, RoleID, LoginIP, LoginTime, status ");
            strSql.Append(" FROM T_Admin_Info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            DingTalkAgent.Model.Admin_Info model;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                model = new DingTalkAgent.Model.Admin_Info();
                if (dr["ID"].ToString() != "")
                {
                    model.ID = int.Parse(dr["ID"].ToString());
                }
                model.AreaCode = dr["AreaCode"].ToString();
                model.Remark = dr["Remark"].ToString();
                model.account = dr["account"].ToString();
                model.password = dr["password"].ToString();
                model.Phone = dr["Phone"].ToString();
                model.Name = dr["Name"].ToString();
                if (dr["RoleID"].ToString() != "")
                {
                    model.RoleID = int.Parse(dr["RoleID"].ToString());
                }
                model.LoginIP = dr["LoginIP"].ToString();
                if (dr["LoginTime"].ToString() != "")
                {
                    model.LoginTime = DateTime.Parse(dr["LoginTime"].ToString());
                }
                if (dr["status"].ToString() != "")
                {
                    model.status = int.Parse(dr["status"].ToString());
                }

                yield return model;
            }
        }
        /// <summary>
        /// 获得数据 T_Admin_Info分页列表
        /// </summary>
        /// <param name="PageIndex">页码</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="total">总记录数</param>
        /// <param name="keyword">关键字</param>
        /// <returns></returns>
        public List<DingTalkAgent.Model.Admin_Info> GetPageList(int PageIndex, int PageSize, out int total, string keyword)
        {
            int Skip = (PageIndex - 1) * PageSize;
            //count
            StringBuilder strSqlCount = new StringBuilder();
            strSqlCount.Append("Select @total=count(ID) From T_Admin_Info ");
            //data
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Top " + PageSize + " * from (");
            strSql.Append("select  row_number() OVER (ORDER BY ID Desc) AS row, ID, AreaCode, Remark, account, password, Phone, Name, RoleID, LoginIP, LoginTime, status ");
            strSql.Append(" FROM  T_Admin_Info ");
            //where
            StringBuilder Where = new StringBuilder();
            Where.Append(" where 1=1");
            if (keyword.Trim() != "")
            {
                Where.Append(" and (account like @keyword or Name like @keyword) ");
            }


            strSqlCount.Append(Where.ToString());
            strSql.Insert(0, strSqlCount.ToString() + ";");
            strSql.Append(Where.ToString());
            strSql.Append(") as a ");
            strSql.Append(" Where a.row>" + Skip);

            //parameters
            SqlParameter[] parameters = {
                    new SqlParameter("@total", SqlDbType.Int,4),
                    new SqlParameter("@keyword", SqlDbType.VarChar,64)
                    };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = "%" + keyword.Trim() + "%";

            List<DingTalkAgent.Model.Admin_Info> list = new List<DingTalkAgent.Model.Admin_Info>();
            DingTalkAgent.Model.Admin_Info model = new DingTalkAgent.Model.Admin_Info();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            total = Convert.ToInt32(parameters[0].Value);

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                model = new DingTalkAgent.Model.Admin_Info();
                if (dr["ID"].ToString() != "")
                {
                    model.ID = int.Parse(dr["ID"].ToString());
                }
                model.AreaCode = dr["AreaCode"].ToString();
                model.Remark = dr["Remark"].ToString();
                model.account = dr["account"].ToString();
                model.password = dr["password"].ToString();
                model.Phone = dr["Phone"].ToString();
                model.Name = dr["Name"].ToString();
                if (dr["RoleID"].ToString() != "")
                {
                    model.RoleID = int.Parse(dr["RoleID"].ToString());
                }
                model.LoginIP = dr["LoginIP"].ToString();
                if (dr["LoginTime"].ToString() != "")
                {
                    model.LoginTime = DateTime.Parse(dr["LoginTime"].ToString());
                }
                if (dr["status"].ToString() != "")
                {
                    model.status = int.Parse(dr["status"].ToString());
                }

                list.Add(model);
            }
            return list;
        }


        #endregion
    }

}
